//
//  XMintroEditView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/14.
//

#import "XMintroEditView.h"

@interface XMintroEditView ()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *wordNumLabel;
@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation XMintroEditView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:self.introTextView];
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), _introTextView.maxY, _introTextView.width - KScaleWidth(28.0)*2, KScaleWidth(41.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    explainLabel.text = @"7天内可以修改3次";
    [self addSubview:explainLabel];
    
    [self addSubview:self.wordNumLabel];
    [self addSubview:self.placeholderLabel];
    
    [_introTextView becomeFirstResponder];
}

- (void)textViewDidChange:(UITextView *)textView {
    _placeholderLabel.hidden = (textView.text.length != 0);
    if (textView.text.length > 100) {
        textView.text = [textView.text substringToIndex:100];
    }
    _wordNumLabel.text = [NSString stringWithFormat:@"%ld/100", textView.text.length];
}

- (void)setContentWithDes:(NSString *)desStr {
    _introTextView.text = desStr;
    _wordNumLabel.text = [NSString stringWithFormat:@"%ld/100", desStr.length];
    _placeholderLabel.hidden = (desStr.length != 0);
}

#pragma mark - Init
- (UITextView *)introTextView {
    if (!_introTextView) {
        _introTextView = [[UITextView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(16.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(120.0))];
        _introTextView.contentInset = UIEdgeInsetsMake(KScaleWidth(10.0), KScaleWidth(10.0), KScaleWidth(10.0), KScaleWidth(10.0));
        _introTextView.backgroundColor = kColorHEX(0xF6F6F6);
        _introTextView.delegate = self;
        [_introTextView setCornerRadius:KScaleWidth(5.0)];
    }
    return _introTextView;
}

- (UILabel *)wordNumLabel {
    if (!_wordNumLabel) {
        _wordNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width - KScaleWidth(30.0) - KScaleWidth(50.0), self.height - KScaleWidth(50.0) - KScaleWidth(33.0), KScaleWidth(50.0), KScaleWidth(33.0)) font:KFontSize(14.0) textColor:kColorHEX(0x999999) textAlignment:NSTextAlignmentRight];
    }
    return _wordNumLabel;
}

- (UILabel *)placeholderLabel {
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(18.0) + KScaleWidth(10.0), KScaleWidth(8.0) + KScaleWidth(16.0), self.introTextView.width - KScaleWidth(10.0)*2 - KScaleWidth(16.0)*2, KScaleWidth(33.0)) font:KFontSize(14.0) textColor:kColorHEX(0x999999)];
        _placeholderLabel.text = @"有兴趣的简介可以吸引粉丝";
    }
    return _placeholderLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
